package com.yinp.proapp.activity;

import android.text.TextUtils;
import android.view.View;

import com.yinp.proapp.activity.bean.MyUserInfo;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.constant.SpConstants;
import com.yinp.proapp.databinding.ActivityLoginBinding;
import com.yinp.proapp.utils.LoadUtils;
import com.yinp.proapp.utils.SpUtils;
import com.yinp.proapp.utils.StatusBarUtil;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

/**
 * 登录界面
 */
public class LoginActivity extends AppBaseActivity<ActivityLoginBinding> {
    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.stvLogin, bd.tvGoRegister);
        bd.header.headerCenterTitle.setText("登录");
    }

    @Override
    public void onClick(View v) {
        if (v == bd.stvLogin) {
            login();
        } else if (v == bd.tvGoRegister) {
            goToActivity(RegisterActivity.class);
        }
    }

    private void login() {
        if (TextUtils.isEmpty(bd.tieAccount.getText())) {
            showToast("还没有填写账号");
            return;
        }
        if (TextUtils.isEmpty(bd.tiePassword.getText())) {
            showToast("还没有填写没密码");
            return;
        }
        MyUserInfo.loginByAccount(bd.tieAccount.getText().toString().trim(), bd.tiePassword.getText().toString().trim(), new LogInListener<MyUserInfo>() {
            @Override
            public void done(MyUserInfo myUserInfo, BmobException e) {
                if (e == null) {
                    SpUtils.saveValue(myUserInfo.getObjectId(), SpConstants.OBJECT_ID);
                    goToActivity(MajorActivity.class);
                } else {
                    //101
                    showToast("用户名或密码不正确");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}